﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class AdminStock : BaseAdminModule
    {
        [AccessedThroughProperty("cmbCategory")]
        private DropDownList _cmbCategory;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        [AccessedThroughProperty("cmdUpdate")]
        private LinkButton _cmdUpdate;
        private string _ctl;
        [AccessedThroughProperty("ctlPagingControl")]
        private AdminPagingControl _ctlPagingControl;
        private int _CurrentPage;
        [AccessedThroughProperty("dgModel")]
        private DataGrid _dgModel;
        [AccessedThroughProperty("lblLineBreak")]
        private Label _lblLineBreak;
        private string _mid;
        [AccessedThroughProperty("pnlList")]
        private Panel _pnlList;
        private string _SkinSrc;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;

        public AdminStock()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            this._CurrentPage = 1;
            this._ctl = "";
            this._mid = "";
            this._SkinSrc = "";
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            try
            {
                this.dgModel.CurrentPageIndex = 0;
                this._CurrentPage = 1;
                SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SearchText", this.txtSearch.Text, 1.0);
                SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "CatId", this.cmbCategory.SelectedValue, 1.0);
                this.populateModel();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.UpdateModel();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgModel_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;
            if (((item.ItemType == ListItemType.Item) | (item.ItemType == ListItemType.AlternatingItem)) | (item.ItemType == ListItemType.SelectedItem))
            {
                TextBox box3 = (TextBox) e.Item.FindControl("txtUnitCost");
                TextBox box = (TextBox) e.Item.FindControl("txtDealerCost");
                TextBox box2 = (TextBox) e.Item.FindControl("txtPurchaseCost");
                Label label = (Label) e.Item.FindControl("lblModelName");
                Label label2 = (Label) e.Item.FindControl("lblProductName");
                if (label2 != null)
                {
                    if (((NB_Store_ModelInfo) e.Item.DataItem).ProductName.Length > 20)
                    {
                        label2.Text = ((NB_Store_ModelInfo) e.Item.DataItem).ProductName.Substring(0, 20);
                        label2.ToolTip = ((NB_Store_ModelInfo) e.Item.DataItem).ProductName;
                    }
                    else
                    {
                        label2.Text = ((NB_Store_ModelInfo) e.Item.DataItem).ProductName;
                    }
                }
                if (label != null)
                {
                    if (((NB_Store_ModelInfo) e.Item.DataItem).ModelName.Length > 20)
                    {
                        label.Text = ((NB_Store_ModelInfo) e.Item.DataItem).ModelName.Substring(0, 20);
                        label.ToolTip = ((NB_Store_ModelInfo) e.Item.DataItem).ModelName;
                    }
                    else
                    {
                        label.Text = ((NB_Store_ModelInfo) e.Item.DataItem).ModelName;
                    }
                }
                if ((box3 != null) && Versioned.IsNumeric(box3.Text))
                {
                    box3.Text = Conversions.ToDouble(box3.Text).ToString();
                }
                if ((box != null) && Versioned.IsNumeric(box.Text))
                {
                    box.Text = Conversions.ToDouble(box.Text).ToString();
                }
                if ((box2 != null) && Versioned.IsNumeric(box2.Text))
                {
                    box2.Text = Conversions.ToDouble(box2.Text).ToString();
                }
            }
        }

        private void dgModel_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgModel.CurrentPageIndex = e.NewPageIndex;
            this.populateModel();
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this._CurrentPage = 1;
            if ((this.Request.QueryString["currentpage"] != null) && Versioned.IsNumeric(this.Request.QueryString["currentpage"]))
            {
                this._CurrentPage = Conversions.ToInteger(this.Request.QueryString["currentpage"]);
            }
            this._ctl = "";
            if (this.Request.QueryString["ctl"] != null)
            {
                this._ctl = this.Request.QueryString["ctl"];
            }
            this._mid = "";
            if (this.Request.QueryString["mid"] != null)
            {
                this._mid = this.Request.QueryString["mid"];
            }
            this._SkinSrc = "";
            if (this.Request.QueryString["SkinSrc"] != null)
            {
                this._SkinSrc = this.Request.QueryString["SkinSrc"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.Page.IsPostBack)
                {
                    SharedFunctions.populateCategoryList(this.PortalId, this.cmbCategory, "-1", DotNetNuke.Services.Localization.Localization.GetString("All", this.LocalResourceFile), "");
                    this.populateModel();
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void populateModel()
        {
            ProductController controller = new ProductController();
            int pageSize = 0x19;
            int num = 0;
            DataGrid dgModel = this.dgModel;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgModel, this.LocalResourceFile);
            this.dgModel = dgModel;
            if (Versioned.IsNumeric(SharedFunctions.GetStoreSetting(this.PortalId, "stockpage.size")))
            {
                pageSize = Conversions.ToInteger(SharedFunctions.GetStoreSetting(this.PortalId, "stockpage.size"));
            }
            if (!this.Page.IsPostBack)
            {
                this.txtSearch.Text = SharedFunctions.getStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SearchText");
                string str = SharedFunctions.getStoreCookieValue(this.PortalSettings.PortalId, this.ID, "CatId");
                if (this.cmbCategory.Items.FindByValue(str) != null)
                {
                    this.cmbCategory.SelectedValue = str;
                }
            }
            ArrayList list = controller.GetModelStockList(this.PortalId, this.txtSearch.Text, SharedFunctions.GetCurrentCulture(), Conversions.ToInteger(this.cmbCategory.SelectedValue), this._CurrentPage, pageSize, SharedFunctions.IsDealer(this.PortalId, this.UserInfo, SharedFunctions.GetCurrentCulture()));
            num = controller.GetModelStockListSize(this.PortalId, this.txtSearch.Text, SharedFunctions.GetCurrentCulture(), Conversions.ToInteger(this.cmbCategory.SelectedValue), SharedFunctions.IsDealer(this.PortalId, this.UserInfo, SharedFunctions.GetCurrentCulture()));
            this.dgModel.DataSource = list;
            this.dgModel.DataBind();
            if ((num <= pageSize) | (pageSize == -1))
            {
                this.ctlPagingControl.Visible = false;
                this.lblLineBreak.Visible = false;
            }
            else
            {
                this.ctlPagingControl.Visible = true;
                this.lblLineBreak.Visible = true;
            }
            this.ctlPagingControl.TotalRecords = num;
            this.ctlPagingControl.PageSize = pageSize;
            this.ctlPagingControl.CurrentPage = this._CurrentPage;
            this.ctlPagingControl.TabID = this.PortalSettings.ActiveTab.TabID;
            this.ctlPagingControl.BorderWidth = 0;
            this.ctlPagingControl.QuerystringParams = "ctl=" + this._ctl + "&mid=" + this._mid;
            this.ctlPagingControl.SkinSrc = this._SkinSrc;
        }

        private void UpdateModel()
        {
            if (this.Page.IsValid)
            {
                IEnumerator enumerator=null;
                ProductController controller = new ProductController();
                bool flag = false;
                try
                {
                    enumerator = this.dgModel.Items.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        DataGridItem current = (DataGridItem) enumerator.Current;
                        flag = false;
                        NB_Store_ModelInfo model = controller.GetModel(Conversions.ToInteger(current.Cells[0].Text), current.Cells[1].Text);
                        if (model != null)
                        {
                            string text = ((TextBox) current.FindControl("txtMaxStock")).Text;
                            if (Versioned.IsNumeric(text) && (model.QtyStockSet != Conversions.ToInteger(text)))
                            {
                                model.QtyStockSet = Conversions.ToInteger(text);
                                flag = true;
                            }
                            text = ((TextBox) current.FindControl("txtQtyRemaining")).Text;
                            if (Versioned.IsNumeric(text) && (model.QtyRemaining != Conversions.ToInteger(text)))
                            {
                                model.QtyRemaining = Conversions.ToInteger(text);
                                if (model.QtyRemaining > model.QtyStockSet)
                                {
                                    model.QtyStockSet = model.QtyRemaining;
                                }
                                flag = true;
                            }
                            text = ((TextBox) current.FindControl("txtUnitCost")).Text;
                            if (Versioned.IsNumeric(text) && (decimal.Compare(model.UnitCost, Conversions.ToDecimal(text)) != 0))
                            {
                                model.UnitCost = Conversions.ToDecimal(text);
                                flag = true;
                            }
                            text = ((TextBox) current.FindControl("txtDealerCost")).Text;
                            if (Versioned.IsNumeric(text) && (decimal.Compare(model.DealerCost, Conversions.ToDecimal(text)) != 0))
                            {
                                model.DealerCost = Conversions.ToDecimal(text);
                                flag = true;
                            }
                            text = ((TextBox) current.FindControl("txtPurchaseCost")).Text;
                            if (Versioned.IsNumeric(text) && (decimal.Compare(model.PurchaseCost, Conversions.ToDecimal(text)) != 0))
                            {
                                model.PurchaseCost = Conversions.ToDecimal(text);
                                flag = true;
                            }
                            CheckBox box = (CheckBox) current.FindControl("chkDealerOnly");
                            if (box.Checked != model.DealerOnly)
                            {
                                model.DealerOnly = box.Checked;
                                flag = true;
                            }
                            if (flag)
                            {
                                model.QtyTrans = 0;
                                controller.UpdateObjModel(model);
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                this.Response.Redirect(this.EditUrl("AdminStock"), true);
            }
        }

        protected virtual DropDownList cmbCategory
        {
            get
            {
                return this._cmbCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cmbCategory = value;
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdUpdate
        {
            get
            {
                return this._cmdUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdUpdate_Click);
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click -= handler;
                }
                this._cmdUpdate = value;
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click += handler;
                }
            }
        }

        protected virtual AdminPagingControl ctlPagingControl
        {
            get
            {
                return this._ctlPagingControl;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ctlPagingControl = value;
            }
        }

        protected virtual DataGrid dgModel
        {
            get
            {
                return this._dgModel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridPageChangedEventHandler handler = new DataGridPageChangedEventHandler(this.dgModel_PageIndexChanged);
                DataGridItemEventHandler handler2 = new DataGridItemEventHandler(this.dgModel_ItemDataBound);
                if (this._dgModel != null)
                {
                    this._dgModel.PageIndexChanged -= handler;
                    this._dgModel.ItemDataBound -= handler2;
                }
                this._dgModel = value;
                if (this._dgModel != null)
                {
                    this._dgModel.PageIndexChanged += handler;
                    this._dgModel.ItemDataBound += handler2;
                }
            }
        }

        protected virtual Label lblLineBreak
        {
            get
            {
                return this._lblLineBreak;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblLineBreak = value;
            }
        }

        protected virtual Panel pnlList
        {
            get
            {
                return this._pnlList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlList = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }
    }
}

